package com.ey12.db.dao;

import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ey12.db.domain.GmUser;

/**
 * Data access object (DAO) for domain model class GmUser.
 * 
 * @see com.ey12.db.domain.GmUser
 * @author MyEclipse Persistence Tools
 */

public class GmUserDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(GmUserDAO.class);
	// property constants
	public static final String USER_TYPE = "userType";
	public static final String USER_NAME = "userName";
	public static final String USER_PASS = "userPass";
	public static final String USER_EMAIL = "userEmail";
	public static final String MOBILE = "mobile";
	public static final String QQ_NUM = "qqNum";
	public static final String MSN_NUM = "msnNum";
	public static final String REG_DATE = "regDate";
	public static final String LAST_LOGIN_DATE = "lastLoginDate";
	public static final String POINT = "point";
	public static final String LEVEL_NUM = "levelNum";
	public static final String IS_VALID = "isValid";
	public static final String IS_CHECKED = "isChecked";
	public static final String PAY_PASS = "payPass";
	public static final String PROTECT_QUESTION = "protectQuestion";
	public static final String BANK_ACCOUNT = "bankAccount";
	public static final String IS_ONLINE = "isOnline";
	public static final String PROTECT_ANSWER = "protectAnswer";
	public static final String PAYTYPE = "paytype";
	public static final String COUNT_BALANCE = "countBalance";
	public static final String IS_MUSIC = "isMusic";
	public static final String DEPOSIT = "deposit";
	public static final String BUYBARGAIN = "buyBargain";
	public static final String BUYBARGAINRATE = "buybargainrate";
	public static final String SALEBARGAIN = "salebargain";
	public static final String SALEBARGAINRATE = "salebargainrate";
	public static final String BUYERMESSAGE_COUNT = "buyerMessagecount";
	public static final String SALERMESSAGE_COUNT = "salerMessagecount";
	public static final String IDCARDNUM = "idCardNum";
	
	protected void initDao() {
		// do nothing
	}

	public void saveOrUpdate(GmUser transientInstance) {
		log.debug("saving GmUser instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(GmUser persistentInstance) {
		log.debug("deleting GmUser instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	

	public GmUser findById(java.lang.Integer id) {
		log.debug("getting GmUser instance with id: " + id);
		try {
			GmUser instance = (GmUser) getHibernateTemplate().get(
					"com.ey12.db.domain.GmUser", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(GmUser instance) {
		log.debug("finding GmUser instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding GmUser instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from GmUser as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUserType(Object userType) {
		return findByProperty(USER_TYPE, userType);
	}

	public List findByUserName(Object userName) {
		return findByProperty(USER_NAME, userName);
	}

	public List findByUserPass(Object userPass) {
		return findByProperty(USER_PASS, userPass);
	}

	public List findByUserEmail(Object userEmail) {
		return findByProperty(USER_EMAIL, userEmail);
	}

	public List findByMobile(Object mobile) {
		return findByProperty(MOBILE, mobile);
	}

	public List findByQqNum(Object qqNum) {
		return findByProperty(QQ_NUM, qqNum);
	}

	public List findByMsnNum(Object msnNum) {
		return findByProperty(MSN_NUM, msnNum);
	}

	public List findByRegDate(Object regDate) {
		return findByProperty(REG_DATE, regDate);
	}

	public List findByLastLoginDate(Object lastLoginDate) {
		return findByProperty(LAST_LOGIN_DATE, lastLoginDate);
	}

	public List findByPoint(Object point) {
		return findByProperty(POINT, point);
	}

	public List findByLevelNum(Object levelNum) {
		return findByProperty(LEVEL_NUM, levelNum);
	}

	public List findByIsValid(Object isValid) {
		return findByProperty(IS_VALID, isValid);
	}

	public List findByIsChecked(Object isChecked) {
		return findByProperty(IS_CHECKED, isChecked);
	}

	public List findByPayPass(Object payPass) {
		return findByProperty(PAY_PASS, payPass);
	}
	public List findByIdCardNum(Object idCardNum) {
		return findByProperty(IDCARDNUM, idCardNum);
	}

	public List findByProtectQuestion(Object protectQuestion) {
		return findByProperty(PROTECT_QUESTION, protectQuestion);
	}

	public List findByBankAccount(Object bankAccount) {
		return findByProperty(BANK_ACCOUNT, bankAccount);
	}

	public List findByIsOnline(Object isOnline) {
		return findByProperty(IS_ONLINE, isOnline);
	}

	public List findByProtectAnswer(Object protectAnswer) {
		return findByProperty(PROTECT_ANSWER, protectAnswer);
	}

	public List findByPaytype(Object paytype) {
		return findByProperty(PAYTYPE, paytype);
	}

	public List findByCountBalance(Object countBalance) {
		return findByProperty(COUNT_BALANCE, countBalance);
	}

	public List findByIsMusic(Object isMusic) {
		return findByProperty(IS_MUSIC, isMusic);
	}

	public List findByDeposit(Object deposit) {
		return findByProperty(DEPOSIT, deposit);
	}

	public List findByBuybargain(Object buyBargain) {
		return findByProperty(BUYBARGAIN, buyBargain);
	}

	public List findByBuybargainrate(Object buyBargainrate) {
		return findByProperty(BUYBARGAINRATE, buyBargainrate);
	}

	public List findBySalebargain(Object saleBargain) {
		return findByProperty(SALEBARGAIN, saleBargain);
	}

	public List findBySalebargainrate(Object saleBargainrate) {
		return findByProperty(SALEBARGAINRATE, saleBargainrate);
	}

	public List findByBuyermessageCount(Object buyerMessagecount) {
		return findByProperty(BUYERMESSAGE_COUNT, buyerMessagecount);
	}

	public List findBySalermessageCount(Object salerMessagecount) {
		return findByProperty(SALERMESSAGE_COUNT, salerMessagecount);
	}



	public List findAll() {
		log.debug("finding all GmUser instances");
		try {
			String queryString = "from GmUser";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findSonUser(String username){
		log.debug("finding all GmUser instances");
		try {
			String queryString = "from GmUser g where g.isValid=1 and g.gmUser.userName='"+username+"' and g.gmUser.isValid=1";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	
	public List findTheMember(String qqNum,String username){
		log.debug("finding all GmUser instances");
		try {
			String queryString = "from GmUser g where g.isValid=1 and g.isRequst=2";
			if(qqNum!=""&&qqNum!=null){
				queryString+=" and g.qqNum='"+qqNum+"'";
			}
			if(username!=""&&username!=null){
				queryString+=" and g.userName='"+username+"'";
			}
			System.out.println(queryString);
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			re.printStackTrace();
			throw re;
		}
	}
	
	public List findUncheckUser(){
		try {
			String queryString = "from GmUser g where g.isValid=1 and g.isRequst=2";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findAllCount(){
		try {
			String queryString = "select sum(g.countBalance) from GmUser g where g.isValid=1";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			re.printStackTrace();
			throw re;
		}
	}
	
	public List findUserList(){
		try {
			String queryString = "from GmUser where userType=1 and isValid=1 order by point desc ";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findSonUserList(GmUser user){
		try {
			String queryString = "from GmUser g where g.userName='"+user.getUserName()+"' and isValid=1 order by point";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findAllMembers(){
		try {
			String queryString = "from GmUser";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findMembersData(String qq, String userName, Integer userType){
		try {
			String queryString = "from GmUser g where  g.isValid=1";
			if(qq!=null&&!"".equals(qq)){
				queryString+=" and g.qqNum='"+qq+"'";
				
			}
			if(userName!=null&&!"".equals(userName)){
				queryString+=" and g.userName='"+userName+"'"; 
			}
			if(userType!=null&&userType!=0){
				queryString+=" and g.levelNum='"+userType+"'";
			}
			System.out.println(queryString);
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public GmUser merge(GmUser detachedInstance) {
		log.debug("merging GmUser instance");
		try {
			GmUser result = (GmUser) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(GmUser instance) {
		log.debug("attaching dirty GmUser instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(GmUser instance) {
		log.debug("attaching clean GmUser instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static GmUserDAO getFromApplicationContext(ApplicationContext ctx) {
		return (GmUserDAO) ctx.getBean("GmUserDAO");
	}
}